package cq.zhcqk.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.app;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.CheckLogin;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.SpUtil;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/12/10.
 */
public class MineFankuiActivity extends BaseActivity implements View.OnClickListener{

    private EditText title;
    private EditText content;
    private Button submit;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_fankui);
        mSharedPreferences = SpUtil.getSpBykey(context, SpUtil.TOKEN);
        initBack();
        title = (EditText) findViewById(R.id.title);
        content = (EditText) findViewById(R.id.content);
        submit = (Button) findViewById(R.id.submit);

        submit.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("我要反馈");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.submit:
                if(!app.isLogin){
                    Toast.makeText(context,"请先登录",Toast.LENGTH_SHORT).show();
                }
                fankui();
                break;
        }
    }

    private void fankui(){
        if(StringUtil.isNull(title.getText()+"")){
            Toast.makeText(context,"请输入标题",Toast.LENGTH_SHORT).show();
            return;
        }
        if(StringUtil.isNull(content.getText()+"")){
            Toast.makeText(context,"请输入内容",Toast.LENGTH_SHORT).show();
            return;
        }
        buildBean.show();
        RequestParams params = new RequestParams();
        params.add("content",content.getText()+"");
        params.add("phone",mSharedPreferences.getString("phone",null));
        params.add("title",title.getText()+"");
        System.out.println(params);
        httpClient.post(Common.URL+Common.FEEDBACK,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                Toast.makeText(context,"反馈成功",Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
                Toast.makeText(context,"反馈失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"反馈失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
